package top.sdaily.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.sdaily.core.mybatis.Conditions;
import top.sdaily.core.utils.IdGeneratorUtils;
import top.sdaily.mapper.RoleMenuMapper;
import top.sdaily.mapper.RoleMenuRightMapper;
import top.sdaily.web.dto.DRoleMenuTreeNode;
import top.sdaily.web.model.*;
import top.sdaily.web.service.RoleMenuRightService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tsujishiki on 2017/1/24.
 */
@Service
@Transactional
public class RoleMenuRightServiceImpl implements RoleMenuRightService {

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Autowired
    RoleMenuRightMapper roleMenuRightMapper;

    @Override
    public List<Role> getAllRoleMenuRight() {
        List<Role> allRole = roleMenuRightMapper.findRoleMenuRight1();
        return allRole;
    }

    @Override
    public List<DRoleMenuTreeNode> getRoleMenuRight(String roleId) {
        return roleMenuMapper.findWithRight(roleId,"0");
    }

    @Override
    public void modifyRoleMenuRight(List<Role> roleMenuRightList) {
        roleMenuRightMapper.delete(new Conditions(RoleMenuRight.class));
        roleMenuMapper.delete(new Conditions(RoleMenu.class));

        List<RoleMenu> newRoleMenuList = new ArrayList<RoleMenu>();
        List<RoleMenuRight> newRoleMenuRightList = new ArrayList<RoleMenuRight>();
        RoleMenu roleMenu = null;
        RoleMenuRight roleMenuRight = null;
        for (int i = 0; i < roleMenuRightList.size(); i++) {
            Role tmpRole = roleMenuRightList.get(i);
            if (tmpRole.getRoleMenus() != null && tmpRole.getRoleMenus().size() > 0) {
                for (int j = 0; j < tmpRole.getRoleMenus().size(); j++) {
                    Menu tmpMenu = tmpRole.getRoleMenus().get(j);
                    String tmpRoleMenuPkid = IdGeneratorUtils.fresh();
                    roleMenu = new RoleMenu();
                    roleMenu.setPkid(tmpRoleMenuPkid);
                    roleMenu.setRoleId(tmpRole.getPkid());
                    roleMenu.setMenuId(tmpMenu.getPkid());
                    newRoleMenuList.add(roleMenu);

                    if (tmpMenu.getRoleMenuRights() != null && tmpMenu.getRoleMenuRights().size() > 0) {
                        for (int k = 0; k < tmpMenu.getRoleMenuRights().size(); k++) {
                            Right tmpRight = tmpMenu.getRoleMenuRights().get(k);
                            String tmpRoleMenuRightPkid = IdGeneratorUtils.fresh();
                            roleMenuRight = new RoleMenuRight();
                            roleMenuRight.setPkid(tmpRoleMenuRightPkid);
                            roleMenuRight.setRoleMenuId(tmpRoleMenuPkid);
                            roleMenuRight.setRightId(tmpRight.getPkid());
                            newRoleMenuRightList.add(roleMenuRight);
                        }
                    }
                }
            }
        }

        roleMenuMapper.insertBatch(newRoleMenuList);
        roleMenuRightMapper.insertBatch(newRoleMenuRightList);
    }

    @Override
    public void modifyRoleMenuRight(List<DRoleMenuTreeNode> roleMenuRight, String roleId) {
        Set<String> parentNode = new HashSet<String>();
        List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
        List<RoleMenuRight> roleMenuRights = new ArrayList<RoleMenuRight>();

        for(DRoleMenuTreeNode roleMenuTreeNode : roleMenuRight){
            parentNode.add(roleMenuTreeNode.getParentId());
            RoleMenu roleMenu = roleMenuSetter(roleMenuTreeNode.getMenuId(), roleId);
            roleMenus.add(roleMenu);
            for (Right right : roleMenuTreeNode.getRights()) {
                roleMenuRights.add(roleMenuRightSetter(right.getPkid(),roleMenu.getPkid()));
            }
        }

        for(String id : parentNode){
            roleMenus.add(roleMenuSetter(id, roleId));
        }

        roleMenuRightMapper.deleteByRoleId(roleId);
        roleMenuMapper.delete(new Conditions(RoleMenu.class).eq("role_id",roleId));

        roleMenuMapper.insertBatch(roleMenus);
        roleMenuRightMapper.insertBatch(roleMenuRights);
    }

    private RoleMenu roleMenuSetter(String menuId,String roleId){
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setPkid(IdGeneratorUtils.fresh());
        roleMenu.setMenuId(menuId);
        roleMenu.setRoleId(roleId);
        return roleMenu;
    }

    private RoleMenuRight roleMenuRightSetter(String rightId,String roleMenuId){
        RoleMenuRight roleMenuRight = new RoleMenuRight();
        roleMenuRight.setPkid(IdGeneratorUtils.fresh());
        roleMenuRight.setRightId(rightId);
        roleMenuRight.setRoleMenuId(roleMenuId);
        return roleMenuRight;
    }
}
